﻿using System;
using System.Collections.Generic;
using System.Text;
using MigratorCore.Bases;
using System.Text.RegularExpressions;
using MigratorCore.Actions;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            MigratorCore.Migrator m = new MigratorCore.Migrator();
            Console.WriteLine("Welcome to The Migrator Console, type help or ? for help");
            Console.WriteLine();
            string line = Console.ReadLine();
            Regex setDP = new Regex("(set-destinationprovider )(\"[\\d\\w\\s]*\")?\\d*", RegexOptions.IgnoreCase);
            Regex setDP2 = new Regex("(set-destinationprovider )|(\"[\\d\\w\\s]*\")?\\d*", RegexOptions.IgnoreCase);
            Regex setDPS = new Regex("(set-destinationprovidersetting)\\s(\"[\\d\\w\\s]*\")?\\d*\\s(\"[\\d\\w\\s\\-\\.;:,_=/\\(\\)\\\\]*\")", RegexOptions.IgnoreCase);
            Regex setDPS2 = new Regex("(set-destinationprovidersetting)\\s|(\"[\\d\\w\\s]*\")?\\d*\\s|(\"[\\d\\w\\s\\-\\.;:,_=/\\(\\)\\\\]*\")", RegexOptions.IgnoreCase);
            Regex setDPAS = new Regex("(set-destinationprovideractionsetting)\\s(\"[\\d\\w\\s]*\")?\\d*\\s(\"[\\d\\w\\s]*\")?\\d*\\s(\"[\\d\\w\\s\\-\\.;:,_=/\\(\\)\\\\]*\")", RegexOptions.IgnoreCase);
            Regex setDPAS2 = new Regex("(set-destinationprovideractionsetting)\\s|(\"[\\d\\w\\s]*\")?\\d*\\s|(\"[\\d\\w\\s]*\")?\\d*\\s|(\"[\\d\\w\\s\\-\\.;:,_=/\\(\\)\\\\]*\")", RegexOptions.IgnoreCase);
            Regex setSP = new Regex("(set-sourceprovider )(\"[\\d\\w\\s]*\")?\\d*", RegexOptions.IgnoreCase);
            Regex setSP2 = new Regex("(set-sourceprovider )|(\"[\\d\\w\\s]*\")?\\d*", RegexOptions.IgnoreCase);
            Regex setSPS = new Regex("(set-sourceprovidersetting)\\s(\"[\\d\\w\\s]*\")?\\d*\\s(\"[\\d\\w\\s\\-\\.;:,_=/\\(\\)\\\\]*\")", RegexOptions.IgnoreCase);
            Regex setSPS2 = new Regex("(set-sourceprovidersetting)\\s|(\"[\\d\\w\\s]*\")?\\d*\\s|(\"[\\d\\w\\s\\-\\.;:,_=/\\(\\)\\\\]*\")", RegexOptions.IgnoreCase);
            Regex setSPAS = new Regex("(set-sourceprovideractionsetting)\\s(\"[\\d\\w\\s]*\")?\\d*\\s(\"[\\d\\w\\s]*\")?\\d*\\s(\"[\\d\\w\\s\\-\\.;:,_=/\\(\\)\\\\]*\")", RegexOptions.IgnoreCase);
            Regex setSPAS2 = new Regex("(set-sourceprovideractionsetting)\\s|(\"[\\d\\w\\s]*\")?\\d*|\\s(\"[\\d\\w\\s]*\")?\\d*\\s|(\"[\\d\\w\\s\\-\\.;:,_=/\\(\\)\\\\]*\")", RegexOptions.IgnoreCase);
            while (line.Length > 0)
            {
                if (line.ToLower() == "help" || line == "?")
                {
                    Console.WriteLine("Commands:");
                    Console.WriteLine("get-DestinationProvider");
                    Console.WriteLine("get-SourceProvider");
                    Console.WriteLine("get-Providers");
                    Console.WriteLine("set-DestinationProvider \"ProviderName\"|Index");
                    Console.WriteLine("set-DestinationProviderSetting \"Name\"|Index Value");
                    Console.WriteLine("set-DestinationProviderActionSetting \"ActionName\"|Index \"Name\"|Index Value");
                    Console.WriteLine("set-SourceProvider \"ProviderName\"|Index");
                    Console.WriteLine("set-SourceProviderSetting \"Name\"|Index Value");
                    Console.WriteLine("set-SourceProviderActionSetting \"ActionName\"|Index \"Name\"|Index Value");
                    Console.WriteLine("start");
                }
                else if (line.ToLower() == "get-providers")
                {
                    int x = 0;
                    foreach (IProvider p in m.Providers)
                    {
                        x++;
                        Console.WriteLine("Provider " + x.ToString() + ": " + p.Name);
                    }
                    Console.WriteLine();
                }
                else if (line.ToLower() == "get-destinationprovider")
                {
                    try
                    {
                        Console.WriteLine("Name: " + m.Destination.Name);
                        Console.WriteLine("Version: " + m.Destination.Version);
                        Console.WriteLine("Settings: ");
                        foreach (string key in m.Destination.Settings.Keys)
                            Console.WriteLine("     " + key + " = " + m.Destination.Settings[key].ToString());
                        int x = 0;
                        Console.WriteLine("Actions: ");
                        foreach (IAction a in m.Destination.Actions)
                            if (a.GetType().GetInterfaces()[0] == typeof(IDestinationAction) || a.GetType().GetInterfaces()[0] == typeof(IFullAction))
                            {
                                x++;
                                Console.WriteLine(string.Format("     {0}: {1} ({2}) with ActionType {3}", x, a.Name, a.GetType().GetInterfaces()[0].Name, a.ActionType));
                                for (int i = 0; i < a.Settings.Count; i++)
                                    Console.WriteLine(string.Format("          Setting {0}: {1} = {2}", i+1, a.Settings.Keys[i], a.Settings[i]));
                            }
                    }
                    catch (Exception e) { Console.WriteLine(e.ToString()); }
                    finally { Console.WriteLine("Done"); }
                }
                else if (line.ToLower() == "get-sourceprovider")
                {
                    try
                    {
                        Console.WriteLine("Name: " + m.Source.Name);
                        Console.WriteLine("Version: " + m.Source.Version);
                        Console.WriteLine("Settings: ");
                        foreach (string key in m.Source.Settings.Keys)
                            Console.WriteLine("     " + key + " = " + m.Source.Settings[key].ToString());
                        int x = 0;
                        Console.WriteLine("Actions: ");
                        foreach (IAction a in m.Source.Actions)
                            if (a.GetType().GetInterfaces()[0] == typeof(ISourceAction) || a.GetType().GetInterfaces()[0] == typeof(IFullAction))
                            {
                                x++;
                                Console.WriteLine(string.Format("     {0}: {1} ({2}) with ActionType {3}", x, a.Name, a.GetType().GetInterfaces()[0].Name, a.ActionType));
                                for (int i = 0; i < a.Settings.Count; i++)
                                    Console.WriteLine(string.Format("          Setting {0}: {1} = {2}", i+1, a.Settings.Keys[i], a.Settings[i]));
                            }
                    }
                    catch (Exception e) { Console.WriteLine(e.ToString()); }
                    finally { Console.WriteLine("Done"); }
                }
                else if (setDP.IsMatch(line))
                {
                    try
                    {
                        string s = setDP2.Matches(line)[1].Value.Replace("\"", "").Trim();
                        int i = -1;
                        if (int.TryParse(s, out i))
                            m.Destination = m.Providers[int.Parse(s) - 1];
                        else
                        {
                            foreach (IProvider p in m.Providers)
                                if (p.Name.ToLower() == s.ToLower())
                                    m.Destination = p;
                        }
                    }
                    catch (Exception e) { Console.WriteLine(e.ToString()); }
                    finally { Console.WriteLine("Done"); }
                }
                else if (setDPS.IsMatch(line))
                {
                    try
                    {
                        string s = setDPS2.Matches(line)[1].Value.Replace("\"", "").Trim();
                        string s2 = setDPS2.Matches(line)[2].Value.Replace("\"", "").Trim();
                        int i = -1;
                        if (int.TryParse(s, out i))
                            m.Destination.saveSetting(m.Destination.Settings.Keys[i - 1], s2);
                        else
                            m.Destination.saveSetting(s, s2);
                    }
                    catch (Exception e) { Console.WriteLine(e.ToString()); }
                    finally { Console.WriteLine("Done"); }
                }
                else if (setDPAS.IsMatch(line))
                {
                    try
                    {
                        string s = setDPAS2.Matches(line)[1].Value.Replace("\"", "").Trim();
                        string s2 = setDPAS2.Matches(line)[2].Value.Replace("\"", "").Trim();
                        string s3 = setDPAS2.Matches(line)[3].Value.Replace("\"", "").Trim();
                        int i, i2;
                        if (int.TryParse(s, out i))
                        {
                            if (int.TryParse(s2, out i2))
                                m.Destination.Actions[i-1].Settings[i2-1] = s3;
                            else m.Destination.Actions[i-1].Settings[s2] = s3;
                        }
                        else
                        {
                            if (int.TryParse(s2, out i2))
                                m.Destination.Actions[m.Destination.Actions.IndexOf(m.Destination.getAction(s))].Settings[i2-1] = s3;
                            else m.Destination.Actions[m.Destination.Actions.IndexOf(m.Destination.getAction(s))].Settings[s2] = s3;
                        }
                    }
                    catch (Exception e) { Console.WriteLine(e.ToString()); }
                    finally { Console.WriteLine("Done"); }
                }
                else if (setSP.IsMatch(line))
                {
                    try
                    {
                        string s = setSP2.Matches(line)[1].Value.Replace("\"", "").Trim();
                        int i = -1;
                        if (int.TryParse(s, out i))
                            m.Source = m.Providers[int.Parse(s) - 1];
                        else
                        {
                            foreach (IProvider p in m.Providers)
                                if (p.Name.ToLower() == s.ToLower())
                                    m.Source = p;
                        }
                    }
                    catch (Exception e) { Console.WriteLine(e.ToString()); }
                    finally { Console.WriteLine("Done"); }
                }
                else if (setSPS.IsMatch(line))
                {
                    try
                    {
                        string s = setSPS2.Matches(line)[1].Value.Replace("\"", "").Trim();
                        string s2 = setSPS2.Matches(line)[2].Value.Replace("\"", "").Trim();
                        int i = -1;
                        if (int.TryParse(s, out i))
                            m.Source.saveSetting(m.Source.Settings.Keys[i-1], s2);
                        else
                            m.Source.saveSetting(s, s2);
                    }
                    catch (Exception e) { Console.WriteLine(e.ToString()); }
                    finally { Console.WriteLine("Done"); }
                }
                else if (setSPAS.IsMatch(line))
                {
                    try
                    {
                        string s = setSPAS2.Matches(line)[1].Value.Replace("\"", "").Trim();
                        string s2 = setSPAS2.Matches(line)[2].Value.Replace("\"", "").Trim();
                        string s3 = setSPAS2.Matches(line)[3].Value.Replace("\"", "").Trim();
                        int i, i2;
                        if (int.TryParse(s, out i))
                        {
                            if (int.TryParse(s2, out i2))
                                m.Source.Actions[i].Settings[i2] = s3;
                            else m.Source.Actions[i].Settings[s2] = s3;
                        }
                        else
                        {
                            if (int.TryParse(s2, out i2))
                                m.Source.Actions[m.Destination.Actions.IndexOf(m.Source.getAction(s))].Settings[i2] = s3;
                            else m.Source.Actions[m.Destination.Actions.IndexOf(m.Source.getAction(s))].Settings[s2] = s3;
                        }
                    }
                    catch (Exception e) { Console.WriteLine(e.ToString()); }
                    finally { Console.WriteLine("Done"); }
                }
                else if (line.ToLower() == "exit")
                    return;
                else if (line.ToLower() == "cls")
                    Console.Clear();
                else if (line.ToLower() == "start")
                    Console.WriteLine("This will start the Migration, or atleast run checks for a migration");
                else Console.WriteLine("Unknown Command (For help type help or ?)");
                line = Console.ReadLine();
            }
        }
    }
}
